'use strict';

import * as moment from 'moment'
import utils from 'src/core/utils';
import mongodbHandle from 'src/handle/mongodbHandle';
import CountersDao from './counterDao';
import { pid } from 'process';

class VersionCheatDao {
    handle: any
    constructor(db) {
        this.handle = db.collection("versionCheat")
        this.handle.createIndex("pID", 1)
    }

    newAndSave = async (pID, data): Promise<any> => {
        let date = new Date().getTime()
        return await this.handle.insertOne({
            pID: pID,
            times: 1,
            data,
            createTime: date,
        });
    }

    findOne = async (query): Promise<any> => {
        return await this.handle.findOne(query)
    }

    find = async (query): Promise<any[]> => {
        return await this.handle.find(query).toArray()
    }

    findByPage = async (query, start: number, size: number): Promise<any[]> => {
        return await this.handle.find(query).skip(start).limit(size).toArray()
    }

    findByPageAndSort = async (query, sort, start, size): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).skip(start).limit(size).toArray()
    }

    findFieldByPageAndSort = async (query, sort, field, start, size): Promise<any[]> => {
        return await this.handle.find(query, field).sort(sort).skip(start).limit(size).toArray()
    }

    findAndSort = async (query, sort): Promise<any[]> => {
        return await this.handle.find(query).sort(sort).toArray()
    }
    //opts  { multi: true}
    updateMany = async (query, data): Promise<any> => {
        return await this.handle.updateMany(query, data);
    }

    updateOne = async (query, data): Promise<any> => {
        return await this.handle.updateOne(query, data);
    }

    count = async (query): Promise<number> => {
        return await this.handle.countDocuments(query)
    }

    remove = async (query): Promise<any> => {
        return await this.handle.remove(query, {
            multi: true
        });
    }
}

export default VersionCheatDao